package AppView;

import Listeners.UtilListeners;
import RuntimeInfo.State;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class UtilSection extends JPanel {
    JPanel[] shower = new JPanel[4];
//    AppModel.CanvasSection canvas;

    public UtilSection() {

        setPreferredSize(new Dimension(200, 0));
        addDrawSection();
        addAlterSection();
        addDefaultColorPanel();

    }

    private void addDrawSection() {
        JLabel jl = new JLabel("绘制图形");
        jl.setPreferredSize(new Dimension(150, 20));

        add(jl);

        JPanel drawBtns = new JPanel(new GridLayout(3, 2, 5, 5));
        drawBtns.setPreferredSize(new Dimension(150, 100));

        JButton line = (JButton) drawBtns.add(new JButton("直线"));
        line.addActionListener(UtilListeners.getDrawLineListener());
        JButton rec = (JButton) drawBtns.add(new JButton("矩形"));
        rec.addActionListener(UtilListeners.getDrawRecListener());
        JButton circle = (JButton) drawBtns.add(new JButton("圆"));
        circle.addActionListener(UtilListeners.getDrawEllipseListener());
        JButton text = (JButton) drawBtns.add(new JButton("文字"));
        text.addActionListener(UtilListeners.getDrawTextListener());

        add(drawBtns);
    }

    /**
     * 为功能板块添加更改信息的组件
     */
    private void addAlterSection() {
        JLabel jl = (JLabel) add(new JLabel("更改属性"));
        jl.setPreferredSize(new Dimension(150, 20));

        JPanel alterBtns = (JPanel) add(new JPanel(new GridLayout(5, 1, 5, 5)));
        alterBtns.setPreferredSize(new Dimension(150, 200));

        JButton incrWeight = (JButton) alterBtns.add(new JButton("提高线条粗细"));
        incrWeight.addActionListener(UtilListeners.getIncrWeightListener());
        JButton decrWeight = (JButton) alterBtns.add(new JButton("减小线条粗细"));
        decrWeight.addActionListener(UtilListeners.getDecrWeightListener());
        JButton delShape = (JButton) alterBtns.add(new JButton("删除图形"));
        delShape.addActionListener(UtilListeners.getDeleteShapeListener());
        JButton select = (JButton) alterBtns.add(new JButton("更多颜色"));
        select.addActionListener(UtilListeners.getSelectColorListener());


    }

    /**
     * 为功能组件添加默认调色板
     */
    private void addDefaultColorPanel() {

        JLabel jl = (JLabel) add(new JLabel("默认调色盘"));

        jl.setPreferredSize(new Dimension(150, 30));
        JPanel colors = (JPanel) add(new JPanel(new GridLayout(3, 5, 0, 0)));
        colors.setPreferredSize(new Dimension(180, 108));
        shower[0] = (JPanel) colors.add(new JPanel());
        shower[1] = (JPanel) colors.add(new JPanel());
        colors.add(getDefaultColorSection(Color.BLACK));
        colors.add(getDefaultColorSection(Color.BLUE));
        colors.add(getDefaultColorSection(Color.CYAN));
        shower[2] = (JPanel) colors.add(new JPanel());
        shower[3] = (JPanel) colors.add(new JPanel());
        colors.add(getDefaultColorSection(Color.DARK_GRAY));
        colors.add(getDefaultColorSection(Color.LIGHT_GRAY));
        colors.add(getDefaultColorSection(Color.MAGENTA));
        colors.add(getDefaultColorSection(Color.ORANGE));
        colors.add(getDefaultColorSection(Color.PINK));
        colors.add(getDefaultColorSection(Color.RED));
        colors.add(getDefaultColorSection(Color.YELLOW));
        colors.add(getDefaultColorSection(Color.WHITE));
        // 设置默认颜色
        for (JPanel jp : shower) {
            jp.setBackground(Color.BLACK);
            jp.addMouseListener(UtilListeners.getDefaultColorSectionListner(jp));
        }

        // 设置默认边框
        shower[0].setBorder(BorderFactory.createMatteBorder(2, 2, 0, 0, new Color(0x36b2e8)));
        shower[1].setBorder(BorderFactory.createMatteBorder(2, 0, 0, 2, new Color(0x36b2e8)));
        shower[2].setBorder(BorderFactory.createMatteBorder(0, 2, 2, 0, new Color(0x36b2e8)));
        shower[3].setBorder(BorderFactory.createMatteBorder(0, 0, 2, 2, new Color(0x36b2e8)));
    }

    private JPanel getDefaultColorSection(Color c) {
        JPanel ret = new JPanel();
        ret.setBackground(c);
        // 为每一个MouseListener都注册鼠标监听器
        ret.addMouseListener(UtilListeners.getDefaultColorSectionListner(ret));
        return ret;
    }

    public void setCurColor(Color c) {
        for (JPanel p : shower) {
            p.setBackground(c);
        }
        State.curColor = c;
        if(State.current!=null){
            State.current.setColor(c);
            State.canvas.repaint();
        }

    }
}
